<template>
	<div class="card__body-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<el-form v-show="state.showSearch" :model="state.queryParams" ref="queryFormRef" inline>
				<el-form-item label="用户昵称" prop="nickname">
					<el-input v-model="state.queryParams.nickname" placeholder="请输入用户昵称" clearable />
				</el-form-item>
				<el-form-item label="注册手机号" prop="mobile">
					<el-input v-model="state.queryParams.mobile" placeholder="请输入注册手机号" clearable @input="onVerifiyNumberIntegerMobile" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="onQuery">
						<el-icon>
							<ele-Search />
						</el-icon>
						查 询
					</el-button>
					<el-button @click="onResetQuery">
						<el-icon>
							<ele-Refresh />
						</el-icon>
						重 置
					</el-button>
				</el-form-item>
			</el-form>
			<el-row :gutter="10" class="top-block">
				<el-col :span="1.5">
					<el-button type="warning" @click="onPlatformUser">导入用户</el-button>
				</el-col>
				<RightToolbar v-model:showSearch="state.showSearch" @queryTable="getList" />
			</el-row>
			<el-table :data="state.tableList" v-loading="state.loading">
				<el-table-column label="用户ID" align="center" prop="id" />
				<el-table-column label="用户头像" align="center" prop="avatar">
					<template #default="{ row }">
						<el-avatar :src="row.avatar" shape="square" />
					</template>
				</el-table-column>
				<el-table-column label="用户昵称" align="center" prop="nickname" />
				<el-table-column label="用户注册手机号" align="center" prop="mobile" />
				<el-table-column label="用户来源" align="center" prop="regist_from" :formatter="formatterRegistFrom" />
				<el-table-column label="邀请人" align="center" prop="invite_name" />
				<el-table-column label="邀请人ID" align="center" prop="invite_id" />
				<el-table-column label="注册时间" align="center" prop="regist_time" :formatter="formatterRegistTime" />
				<el-table-column label="操作" align="center" prop="operation" fixed="right">
					<template #default="{ row }">
						<el-button type="primary" link @click="onEditReferrer(row)"> 修改推荐人 </el-button>
						<el-popconfirm title="您确定升级该用户为铜牌会员吗？此操作不可撤回" @confirm="onUpgrade(row)" width="200px">
							<template #reference>
								<el-button type="primary" link> 升级铜牌会员 </el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<PaginationComponent
				v-if="state.total"
				:total="state.total"
				v-model:page="state.queryParams.pageNum"
				v-model:limit="state.queryParams.pageSize"
				@pagination="getList"
			/>
		</el-card>
		<EditReferrerDialog ref="editReferrerDialogRef" />
		<PlatformUserImportDialog ref="platformUserImportDialogRef" />
	</div>
</template>

<script setup lang="ts" name="platformUser">
import { ElMessage } from 'element-plus';
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { usersApi } from '/@/api';
import { timeDictionary } from '/@/utils/dictionary';
import { verifiyNumberInteger1 } from '/@/utils/toolsValidate';

const EditReferrerDialog = defineAsyncComponent(() => import('./dialog/editReferrerDialog.vue'));
const PlatformUserImportDialog = defineAsyncComponent(() => import('./dialog/platformUserImportDialog.vue'));

const UsersApi = usersApi();
// 定义变量内容
const state = reactive({
	showSearch: true,
	queryParams: {
		nickname: '',
		mobile: '',
		identity: '0',
		pageNum: 1,
		pageSize: 10,
	},
	tableList: [],
	loading: false,
	total: 0,
});
const queryFormRef = ref();
const editReferrerDialogRef = ref();
const platformUserImportDialogRef = ref();

onMounted(() => {
	getList();
});

/********** 触发事件 **********/
// 注册手机号过滤
const onVerifiyNumberIntegerMobile = (val: string) => {
	state.queryParams.mobile = verifiyNumberInteger1(val);
};
// 查询
const onQuery = () => {
	state.queryParams.pageNum = 1;
	getList();
};
// 重置查询
const onResetQuery = () => {
	queryFormRef.value.resetFields();
	onQuery();
};
// 导入平台用户
const onPlatformUser = () => {
	platformUserImportDialogRef.value.openDialog(() => getList());
}
// 修改推荐人
const onEditReferrer = (row: Object) => {
	editReferrerDialogRef.value.openDialog(row, () => getList());
};
// 升级铜牌会员
const onUpgrade = (row: any) => {
	_usersUpgradeOne({ user_id: row.id });
};

/********** 方法事件 **********/
// 刷新列表
const getList = () => {
	state.loading = true;
	_usersList(state.queryParams);
};
// 用户来源字典
const formatterRegistFrom = (row: any) => {
	return row.regist_from === 1 ? '用户注册' : 'APP同步';
};
// 注册时间字典
const formatterRegistTime = (row: any) => {
	return timeDictionary(row.regist_time);
};

/********** 网络事件 **********/
// 用户列表
const _usersList = (params: Object) => {
	UsersApi.usersList(params)
		.then((res) => {
			state.tableList = res.data.list;
			state.total = res.data.info.total;
			state.loading = false;
		})
		.catch(() => (state.loading = false));
};
// 普通用户升级为铜牌会员
const _usersUpgradeOne = (params: Object) => {
	UsersApi.usersUpgradeOne(params).then(() => {
		ElMessage.success('操作成功！');
		getList();
	});
};
</script>
